import { defineStore } from "pinia";
import { store } from "..";

export const useUserStore = defineStore({
  id: "app",
  state: () => ({
    sidebar: { opened: false },
<<<<<<< HEAD
    activePath: null
=======
    activePath: null,
    route: null,
    tags: [
      {
        fullPath: "/dashboard",
        meta: { title: "首页" },
        name: "dashboard",
      },
    ],
>>>>>>> lz
  }),
  getters: {},
  actions: {
    toggleSideBar() {
      this.$patch(state => {
        state.sidebar.opened = !state.sidebar.opened;
      });
<<<<<<< HEAD
    }
  }
=======
    },
    // 存储路由标签信息
    storeRouteTags(payload) {
      const { fullPath, meta, name } = payload;
      if (fullPath === "/") return;
      const { tags } = this;

      // 如果大于10个tag，且当前的这个要存的tag是新加入的，则移除在第一个tag
      if (tags.length >= 10 && !tags.find(tag => tag.fullPath === fullPath)) {
        tags.shift();
      }

      // 储存
      if (!tags.find(tag => tag.fullPath === fullPath)) {
        tags.push({ fullPath, meta, name });
      }
    },
    // 移除tag
    removeRouteTags(payload) {
      const { fullPath, meta, name } = payload;
      const { tags } = this;
      const delIndex = tags.findIndex(tag => tag.fullPath === fullPath);
      tags.splice(delIndex, 1);
    },
    // 清空tag信息
    clearRouteTags() {
      const { tags } = this;
      tags.length = 0;
    },
  },
>>>>>>> lz
});

export const useAppStoreWidthOut = () => useUserStore(store);
